package com.icen.utils.comm;

import java.util.Map;
import java.util.Map.Entry;

/**
 * Map Utils Class
 * 
 * @author IcenHan
 * 
 */
public class MapUtils {

	/**
	 * 获取Map集合的大小
	 * 
	 * @param map
	 * @return int
	 */
	public static <K, V> int getSize(Map<K, V> map) {
		return map == null ? 0 : map.size();
	}

	public static <V> boolean putNotEmptyKey(Map<String, V> map, String key, V value) {
		if (map == null || ValidateUtils.isEmpty(key)) {
			return false;
		}
		map.put(key, value);
		return true;
	}

	public static <K, V> boolean putNotNullKey(Map<K, V> map, K key, V value) {
		if (map == null || key == null) {
			return false;
		}
		map.put(key, value);
		return true;
	}

	public static <K, V> boolean putNotNullKeyAndValue(Map<K, V> map, K key,
			V value) {
		if (map == null || key == null || value == null) {
			return false;
		}

		map.put(key, value);
		return true;
	}

	public static <K, V> K getKeyByValue(Map<K, V> map, V value) {
		if (ValidateUtils.isEmpty(map)) {
			return null;
		}

		for (Entry<K, V> entry : map.entrySet()) {
			if (ValidateUtils.isEquals(entry.getValue(), value)) {
				return entry.getKey();
			}
		}
		return null;
	}

}
